﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LoginForm : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
    SqlCommand com;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        con.Open();
        string strCheckUser = "select count(*) from StudentAccount where stu_username = '" + txtUsername.Text.Trim() + "'";
        com = new SqlCommand(strCheckUser, con);
        int n = Convert.ToInt32(com.ExecuteScalar().ToString());
        con.Close();

        if (n == 1)
        {
            con.Open();
            string strCheckPass = "select stu_password from StudentAccount where stu_username = '" + txtUsername.Text.Trim() + "'";
            SqlCommand command2 = new SqlCommand(strCheckPass, con);
            string pass = command2.ExecuteScalar().ToString().Replace(" ", "");
            if (pass == txtPassword.Text)
            {
                
                Session["username"] = txtUsername.Text;
                Response.Redirect("Home2.aspx");
            }

            else
            {
                lblMsgLogin.Text = "Password Error, Try Again!";
            }
        }
        else
        {
            lblMsgLogin.Text = "Username Error, Try Again!";
        }
    }
}